<template>
	<view class="container">
		<view class="form-box">
			<!-- 商品信息 -->
			<view class="goods-section">
				<view class="section-title">
					<text class="label">购买商品</text>
					<text class="add-btn" @tap="addItem">添加商品</text>
				</view>
				
				<view class="goods-list" v-if="formData.items.length > 0">
					<view class="goods-item" v-for="(item, index) in formData.items" :key="index">
						<view class="item-content">
							<input 
								type="text" 
								v-model="item.name" 
								placeholder="商品名称"
								class="item-name"
							/>
							<input 
								type="number" 
								v-model="item.quantity" 
								placeholder="数量"
								class="item-quantity"
							/>
							<input 
								type="digit" 
								v-model="item.price" 
								placeholder="预估单价"
								class="item-price"
							/>
						</view>
						<text class="delete-btn" @tap="deleteItem(index)">删除</text>
					</view>
				</view>
				
				<view class="empty-tip" v-else>
					请添加需要购买的商品
				</view>
			</view>

			<!-- 购买地点 -->
			<view class="form-item">
				<text class="label">购买地点</text>
				<view class="location-select" @tap="selectShopLocation">
					<text>{{formData.shopLocation || '请选择购买地点'}}</text>
					<uni-icons type="right" size="16" color="#999"></uni-icons>
				</view>
			</view>

			<!-- 配送信息 -->
			<view class="delivery-info">
				<view class="form-item">
					<text class="label">送达地点</text>
					<view class="location-select" @tap="selectDeliveryLocation">
						<text>{{formData.deliveryLocation || '请选择送达地点'}}</text>
						<uni-icons type="right" size="16" color="#999"></uni-icons>
					</view>
				</view>

				<view class="form-item">
					<text class="label">期望送达时间</text>
					<picker mode="time" :value="formData.expectedTime" @change="onTimeChange">
						<view class="picker">
							<text>{{formData.expectedTime || '请选择时间'}}</text>
							<uni-icons type="right" size="16" color="#999"></uni-icons>
						</view>
					</picker>
				</view>

				<view class="form-item">
					<text class="label">备注信息</text>
					<textarea v-model="formData.remark" placeholder="请输入备注信息（选填）" />
				</view>
			</view>

			<!-- 费用明细 -->
			<view class="price-section">
				<view class="price-item">
					<text class="label">商品总价</text>
					<text class="price">¥{{goodsTotal}}</text>
				</view>
				<view class="price-item">
					<text class="label">跑腿费</text>
					<text class="price">¥{{serviceFee}}</text>
				</view>
				<view class="price-item">
					<text class="label">加急服务</text>
					<switch :checked="formData.isUrgent" @change="onUrgentChange" color="#2979ff" />
				</view>
				<view class="price-item total">
					<text class="label">预估总价</text>
					<text class="price">¥{{totalPrice}}</text>
				</view>
				<view class="price-tip">
					* 实际费用以购买时的实际价格为准
				</view>
			</view>
		</view>

		<!-- 发布按钮 -->
		<view class="submit-btn" @tap="submitOrder">
			发布订单
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			formData: {
				items: [],
				shopLocation: '',
				deliveryLocation: '',
				expectedTime: '',
				remark: '',
				isUrgent: false
			},
			serviceFee: 5,
			urgentFee: 5
		}
	},
	computed: {
		goodsTotal() {
			return this.formData.items.reduce((total, item) => {
				const price = parseFloat(item.price) || 0
				const quantity = parseInt(item.quantity) || 0
				return total + (price * quantity)
			}, 0).toFixed(2)
		},
		totalPrice() {
			return (parseFloat(this.goodsTotal) + 
				this.serviceFee + 
				(this.formData.isUrgent ? this.urgentFee : 0)
			).toFixed(2)
		}
	},
	methods: {
		addItem() {
			this.formData.items.push({
				name: '',
				quantity: '',
				price: ''
			})
		},
		deleteItem(index) {
			this.formData.items.splice(index, 1)
		},
		selectShopLocation() {
			uni.showActionSheet({
				itemList: ['校内超市', '学生食堂', '水果店', '便利店'],
				success: (res) => {
					this.formData.shopLocation = ['校内超市', '学生食堂', '水果店', '便利店'][res.tapIndex]
				}
			})
		},
		selectDeliveryLocation() {
			uni.showActionSheet({
				itemList: ['宿舍楼', '教学楼', '图书馆', '食堂'],
				success: (res) => {
					this.formData.deliveryLocation = ['宿舍楼', '教学楼', '图书馆', '食堂'][res.tapIndex]
				}
			})
		},
		onTimeChange(e) {
			this.formData.expectedTime = e.detail.value
		},
		onUrgentChange(e) {
			this.formData.isUrgent = e.detail.value
		},
		submitOrder() {
			// 表单验证
			if (this.formData.items.length === 0) {
				uni.showToast({
					title: '请添加购买商品',
					icon: 'none'
				})
				return
			}
			
			// 验证每个商品的信息是否完整
			const invalidItem = this.formData.items.find(item => 
				!item.name || !item.quantity || !item.price
			)
			if (invalidItem) {
				uni.showToast({
					title: '请完善商品信息',
					icon: 'none'
				})
				return
			}
			
			if (!this.formData.shopLocation) {
				uni.showToast({
					title: '请选择购买地点',
					icon: 'none'
				})
				return
			}
			if (!this.formData.deliveryLocation) {
				uni.showToast({
					title: '请选择送达地点',
					icon: 'none'
				})
				return
			}
			if (!this.formData.expectedTime) {
				uni.showToast({
					title: '请选择期望送达时间',
					icon: 'none'
				})
				return
			}

			// TODO: 发送请求保存订单
			uni.showLoading({
				title: '发布中...'
			})

			setTimeout(() => {
				uni.hideLoading()
				uni.showToast({
					title: '发布成功',
					icon: 'success'
				})
				// 返回首页
				uni.switchTab({
					url: '/pages/index/index'
				})
			}, 1500)
		}
	}
}
</script>

<style lang="scss">
.container {
	min-height: 100vh;
	background-color: $background-color;
	padding-bottom: 120rpx;
}

.form-box {
	background-color: #fff;
	padding: 20rpx;
	margin-top: 20rpx;
}

.goods-section {
	margin-bottom: 30rpx;

	.section-title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;

		.label {
			font-size: 28rpx;
			color: $text-color;
		}

		.add-btn {
			font-size: 28rpx;
			color: $theme-color;
		}
	}

	.goods-list {
		.goods-item {
			background-color: #f5f5f5;
			padding: 20rpx;
			border-radius: 8rpx;
			margin-bottom: 20rpx;

			.item-content {
				display: flex;
				margin-bottom: 10rpx;

				input {
					background-color: #fff;
					padding: 10rpx;
					border-radius: 4rpx;
					font-size: 28rpx;
					margin-right: 10rpx;

					&.item-name {
						flex: 2;
					}

					&.item-quantity {
						flex: 1;
					}

					&.item-price {
						flex: 1;
					}
				}
			}

			.delete-btn {
				color: #ff5500;
				font-size: 28rpx;
				text-align: right;
				display: block;
			}
		}
	}

	.empty-tip {
		text-align: center;
		color: #999;
		font-size: 28rpx;
		padding: 40rpx 0;
	}
}

.form-item {
	margin-bottom: 30rpx;

	.label {
		font-size: 28rpx;
		color: $text-color;
		margin-bottom: 20rpx;
		display: block;
	}

	.location-select, .picker {
		width: 100%;
		background-color: #f5f5f5;
		padding: 20rpx;
		border-radius: 8rpx;
		font-size: 28rpx;
		color: $text-color;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	textarea {
		width: 100%;
		height: 160rpx;
		background-color: #f5f5f5;
		padding: 20rpx;
		border-radius: 8rpx;
		font-size: 28rpx;
	}
}

.price-section {
	background-color: #fff;
	padding: 20rpx;
	margin-top: 20rpx;

	.price-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;

		.label {
			font-size: 28rpx;
			color: $text-color;
		}

		.price {
			font-size: 32rpx;
			color: #ff5500;
			font-weight: bold;
		}

		&.total {
			margin-top: 30rpx;
			padding-top: 20rpx;
			border-top: 1rpx solid $border-color;
		}
	}

	.price-tip {
		font-size: 24rpx;
		color: #999;
		margin-top: 10rpx;
	}
}

.submit-btn {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	height: 100rpx;
	background-color: $theme-color;
	color: #fff;
	font-size: 32rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}
</style> 